<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class UsersController extends AppController {

    var $name = 'Users';
    var $components = array(
        'Session'
    );
    var $uses   =   array(
        'User', 'Usergroup'
    );
    var $helpers = array(
        'Published'
    );
    function index() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Users management');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search =   !empty($this->params['form']['search']) ?   $this->params['form']['search'] :   false;
        $conditions =   array();
        
        //search
        if($search){
            $conditions['OR']   =   array(
                'username LIKE'  =>  "%$search%",
                'email LIKE'     =>  "%$search%",
            );
        }
        $this->paginate = array(
            'conditions'   =>  $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'User.id' => 'DESC'
            ),
        );
        $data = $this->paginate('User');
        $this->set('total', $this->params['paging']['User']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->User->alias}.{$this->User->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        if ($this->User->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Users", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->User->id = $params['cid'][0];
        } else {
            $this->User->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
        if (empty($this->data)) {
            $fields = array(
                '*'
            );
            $this->set('list_group', $this->Usergroup->getListAll());
            $this->data = $this->User->read($fields);
        } 
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - XUsers management');
        $this->set('list_group', $this->Usergroup->getListAll());
    }

    function save() {
        $data = $this->data['User'];
        //no change password
        if (empty($this->data['User']['password'])) {
            unset($data['password']);
            unset($data['password2']);
        }
        if ($this->User->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->User->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }

    function login() {
        if($this->data){
            if($this->Auth->login($this->data)){
                $this->redirect(array('controller' => 'users', 'action' => 'index'));
            }else{
                $this->redirect(array('controller' => 'users', 'action' => 'login'));
            }
        }
        $this->set('title_for_layout', Configure::read('site_name') . ' - Login');
        $this->layout = 'login';
    }

    function cancel() {
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }
    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->User->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->User->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->User->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->User->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }
}

?>
